package com.gmail.hongyz.myfuse.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.catalina.util.RequestUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.bind.RequestUtils;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.gmail.hongyz.myfuse.common.PageObject;
import com.gmail.hongyz.myfuse.model.User;
import com.gmail.hongyz.myfuse.service.UserService;

public class PagingGridAction extends MultiActionController {

	private static final Log log = LogFactory.getLog(PagingGridAction.class);
	
	private UserService userService;
	
	public ModelAndView init(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
	
		if (log.isDebugEnabled()) {
	        log.debug("entering 'handleRequest' method...");
	    }
		
		String queryString = request.getQueryString();
		log.info("query: " + queryString);
		return new ModelAndView("demo/paging-grid");
	}

	public void query(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		if (log.isDebugEnabled()) {
            log.debug("entering 'handleRequest' method...");
        }
		
		String queryString = request.getQueryString();
		log.info("query: " + queryString);
		log.info("start: " + request.getParameter("start"));
		log.info("limit: " + request.getParameter("limit"));
		log.info("total: " + request.getParameter("total"));
		
		int start = 0;
		int limit = 10;
		try {
			start = Integer.parseInt(request.getParameter("start"));
			limit = Integer.parseInt(request.getParameter("limit"));
		} catch (NumberFormatException e) {
		}

		
		
		List<User> users = getTestUsers();
		int totalCount = users.size();
		users = users.subList(start, start + limit);
		
		PageObject pageObject = PageObject.parseParams(request);
		users = userService.getUsers(pageObject);
		log.info("pageObject.total: " + pageObject.getTotal());
		log.info("users: " + users);
		
		JSONObject result = new JSONObject();
		JSONArray list = JSONArray.fromObject(users);
//		result.accumulate("totalCount", totalCount);
		result.accumulate("content", list);
		result.accumulate("totalCount", pageObject.getTotal());
		response.getWriter().println(result.toString());
		response.getWriter().flush();
		response.getWriter().close();
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	private List<User> getTestUsers() {
		List<User> users = new ArrayList<User>();
		for (int i = 0; i < 100; i++) {
			users.add(new User("" + i, "A" + i));
		}
		return users;
	}
}
